﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PCLawPrototype
{
    public partial class NewMatterFrm : EditForm
    {
        List<Client> _clients = new List<Client>();
        private Matter _editMatter;
        private bool isUpdated = false;
        private Contact _contact;
        private List<Lawyer> _lawyers = new List<Lawyer>();
        private List<Lawyer> _inChargeLawyersOld = new List<Lawyer>();
        private List<Lawyer> _inChargeLawyersNew = new List<Lawyer>();

        public NewMatterFrm()
        {
            InitializeComponent();

            FillClientList();
            FillLawyerList(_inChargeLawyersOld);
        }

        public NewMatterFrm(Matter matter)
        {
            InitializeComponent();
            this.Text = "Update Matter Detail [" + matter.MatterID + "-" + matter.Client.ClientName + "]";

            FillClientList();
            _editMatter = matter;

            cbxClient.SelectedIndex = _clients.IndexOf(matter.Client);
            //txtMatterCode.Text = matter.MatterCode;
            dtpOpenedDate.Value = matter.MatterOpenedDate;
            txtDescription.Text = matter.MatterDescription;
            txtAdvancedPayment.Text = matter.MatterAdvancePayment.ToString();

            matter.LawyerInChargeOfMatters.Load();
            foreach (LawyerInChargeOfMatter lawyeric in matter.LawyerInChargeOfMatters)
            {
                lawyeric.LawyerReference.Load();

                FillLawyerToGrid(lawyeric.Lawyer);

                _inChargeLawyersOld.Add(lawyeric.Lawyer);
            }

            matter.Client.ContactReference.Load();
            FillInformation(matter.Client.Contact);
            FillLawyerList(_inChargeLawyersOld);
            isUpdated = true;
        }

        private void FillLawyerToGrid(Lawyer lawyer)
        {
            lawyer.LawyerRanks.Load();
            System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem(new string[] {
            lawyer.LawyerName,
            lawyer.LawyerCode,
            lawyer.LawyerRanks.First(lawy => lawy.Status == 1).RankValue,
            lawyer.LawyerID}, -1);

            this.lstLawyer.Items.Add(listViewItem1);
        }

        private void FillLawyerList(List<Lawyer> inChargeLawyers)
        {
            //_lawyers = Common.Entity.Lawyers.TakeWhile(lawyer => !inChargeLawyers.Contains(lawyer)).ToList();
            _lawyers = Common.Entity.Lawyers.ToList();

            foreach (Lawyer lawyer in inChargeLawyers)
            {
                _lawyers.Remove(lawyer);
            }
            foreach (Lawyer lawyer in _lawyers)
            {
                cbxLawyer.Items.Add(lawyer.LawyerCode + " - " + lawyer.LawyerName);
            }
        }

        private void FillClientList()
        {
            _clients = Common.Entity.Clients.ToList();

            foreach (Client client in _clients)
            {
                cbxClient.Items.Add(client.ClientCode + " - " + client.ClientName);
            }
        }

        private void FillInformation(Contact contact)
        {
            txtTitle.Text = contact.ContactPosition;
            txtFirstname.Text = contact.ContactFirstName;
            txtLastName.Text = contact.ContactLastName;
            txtMiddleName.Text = contact.ContactMiddleName;
            txtStreet.Text = contact.ContactAddress;
            txtCity.Text = contact.ContactCity;
            txtState.Text = contact.ContactState;
            txtZip.Text = contact.ContactZip;
            txtCountry.Text = contact.ContactCountry;
            txtAttention.Text = contact.ContactAttention;
            txtBusinessPhone.Text = contact.ContactBusinessPhone;
            txtHomePhone.Text = contact.ContactHomePhone;
            txtCellPhone.Text = contact.ContactCellPhone;
            txtOtherPhone.Text = contact.ContactOther;
            txtEmail1.Text = contact.ContactEmail1;
            txtEmail2.Text = contact.ContactEmail2;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("Data has been saved!");
            float tmp;
            if (isUpdated)
            {
                _editMatter.Client = _clients[cbxClient.SelectedIndex];
                _editMatter.MatterDescription = txtDescription.Text;
                _editMatter.MatterOpenedDate = dtpOpenedDate.Value;
                _editMatter.MatterAdvancePayment = float.TryParse(txtAdvancedPayment.Text, out tmp)
                                                       ? tmp
                                                       : 0.0;
                _editMatter.UpdatedDate = DateTime.Now;
                foreach (Lawyer lawyer in _inChargeLawyersNew)
                {
                    _editMatter.LawyerInChargeOfMatters.Add(new LawyerInChargeOfMatter()
                    {
                        Matter = _editMatter,
                        Lawyer = lawyer,
                        CreatedDate = DateTime.Now,
                        UpdatedDate = DateTime.Now
                    });
                }

            }
            else
            {
                Matter matter = new Matter()
                                 {
                                     Client = _clients[cbxClient.SelectedIndex],
                                     MatterAdvancePayment =
                                         float.TryParse(txtAdvancedPayment.Text, out tmp)
                                             ? tmp
                                             : 0.0,
                                     MatterDescription = txtDescription.Text,
                                     MatterID = Guid.NewGuid().ToString(),
                                     MatterStatus = 1,
                                     CreatedDate = DateTime.Now,
                                     MatterOpenedDate = dtpOpenedDate.Value,
                                     UpdatedDate = DateTime.Now
                                 };
                foreach (Lawyer lawyer in _inChargeLawyersNew)
                {
                    matter.LawyerInChargeOfMatters.Add(new LawyerInChargeOfMatter()
                    {
                        Matter = matter,
                        Lawyer = lawyer,
                        CreatedDate = DateTime.Now,
                        UpdatedDate = DateTime.Now
                    });
                }

                Common.Entity.AddToMatters(matter);
            }
            Common.Entity.SaveChanges();
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAddToList_Click(object sender, EventArgs e)
        {
            if (cbxLawyer.SelectedIndex > -1)
            {
                _inChargeLawyersNew.Add(_lawyers[cbxLawyer.SelectedIndex]);
                FillLawyerToGrid(_lawyers[cbxLawyer.SelectedIndex]);
                _lawyers.RemoveAt(cbxLawyer.SelectedIndex);
                cbxLawyer.Items.RemoveAt(cbxLawyer.SelectedIndex);
                cbxLawyer.SelectedIndex = -1;
            }
        }

        private void btnSearchLawyer_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Lawyer's list filtered!");
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Help will be shown!");
        }

        private void cbxClient_SelectedIndexChanged(object sender, EventArgs e)
        {
            var tmp = _clients[cbxClient.SelectedIndex];
            tmp.ContactReference.Load();

            FillInformation(tmp.Contact);
        }
    }
}
